import 'dart:math';

import 'package:flame/components.dart';
import 'package:flutter_flame/config/GameConfig.dart';

import '../../../../config/AssetsName.dart';
import '../BodyShadow.dart';
import 'IdleZombie.dart';

class IdleZombieManager extends Component with GameConfig {
  IdleZombieManager();

  void spawnIdleZombies() {
    for (final pos in idleZombiePositions) {
      final zombie = _createIdleZombie(pos);
      add(zombie);
    }
  }

  // 放置僵尸
  IdleZombie _createIdleZombie(Vector2 position) {
    final state = idleZombieState[Random().nextInt(idleZombieState.length)];
    final zombie = IdleZombie(state, setPosition: position);
    zombie.loaded.then((_) {
      zombie.bodyShadow = BodyShadow(
        AssetsName.spriteShadow,
        bodySize: zombie.size,
        bodyPosition: zombie.position + idleZombieShadowOffset,
      );
      add(zombie.bodyShadow);
    });
    return zombie;
  }

  void removeIdleZombies() {
    children.whereType<IdleZombie>().forEach((z) => z.removeFromParent());
  }
}
